package com.toune.movie.mvp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.ta.utdid2.android.utils.NetworkUtils;
import com.toune.movie.R;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.BasePresenterImpl;
import com.vondear.rxtool.view.RxToast;

import butterknife.BindView;

public class RegisteActivity extends BaseActivity {

    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.email_sign_in_button)
    Button emailSignInButton;
    @BindView(R.id.login_form)
    ScrollView loginForm;
    @BindView(R.id.et_email)
    AutoCompleteTextView etEmail;
    @BindView(R.id.ensure_password)
    EditText ensurePassword;
    private String unicomPhoneNumber;

    private void checklogin(String mobile) {

        AVUser avUser = new AVUser();
        avUser.setMobilePhoneNumber(mobile);
        avUser.setUsername(mobile);
        avUser.put("nick", mobile);
        avUser.setPassword(password.getText().toString());
        avUser.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    // 注册成功，把用户对象赋值给当前用户 AVUser.getCurrentUser()
                    finish();
                } else {
                    // 失败的原因可能有多种，常见的是用户名已经存在。
                    RxToast.error(e.getMessage());
                }
            }
        });
    }

    @Override
    public BasePresenterImpl initPresenter() {
        return null;
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {

    }

    @Override
    public void init(Bundle savedInstanceState) {

        emailSignInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unicomPhoneNumber = etEmail.getText().toString();
                if ((unicomPhoneNumber == null) || (unicomPhoneNumber.isEmpty())) {
                    RxToast.info("手机号输入不能为空");
                    return;
                }
                if (password.getText().toString() == null || password.getText().toString().isEmpty()) {
                    RxToast.info("密码不能为空");
                    return;
                }
                if (!password.getText().toString().equals(ensurePassword.getText().toString())) {
                    RxToast.info("两次密码输入不一致");
                    return;
                }
                if (!NetworkUtils.isConnected(context)) {
                    RxToast.info("网络连接异常，请稍后重试");
                    return;
                }
                checklogin(unicomPhoneNumber);
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_registe;
    }

    @Override
    protected String getTitleStr() {
        return "注册";
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }
}
